//
//  LMShareView.m
//  LMWebViewDemo
//
//  Created by 卢蒙蒙 on 16/05/16.
//  Copyright (c) 2015年 TaiHe. All rights reserved.
//

#import "LMShareView.h"
#import "Masonry.h"


#define  WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT  [UIScreen mainScreen].bounds.size.height


#define SHARE_VIEW_HEIGHT 249
#define SHAREBUTTON_TAG 100


static NSTimeInterval  showInterVal = 0.3;


@interface LMShareView ()


/**
 *  回调block
 */
@property(nonatomic,copy) shareBtnClickBlock clickblocks;


/**
 *  图片的数组
 */
@property(nonatomic,strong)NSArray *imageArray;


/**
 *  文字的数组
 */
@property(nonatomic,strong)NSArray *titleArray;


/**
 *  分享视图
 */
@property(nonatomic,strong)UIView *customShareView;


@end

@implementation LMShareView


#pragma mark - lifeCycle

- (instancetype)init {
    
    self = [super init];
    if (self) {

    }
    
    return self;
    
}


-(instancetype)initWithImageNameArray:(NSArray*) imageArray titleArray:(NSArray*) titleArray clickAtIndex:(shareBtnClickBlock) clickBlocks
{
    self = [super init];
    if (self) {
        
        self.imageArray = @[@"trWeixin1",@"trWechatssion",@"trWeibo1",@"trQQ1",@"trQQzone1",@"trCopy link1"];
        self.titleArray = @[@"微信好友",@"微信朋友圈",@"微博",@"QQ",@"QQ空间",@"复制"];
        if (imageArray) {
           self.imageArray = imageArray;
        }
        
        if (titleArray) {
           self.titleArray = titleArray;
        }
        self.clickblocks = clickBlocks;
        
        [self createContentView];
    }
    
    return self;
    
}


-(id)initWithCoder:(NSCoder *)aDecoder{
    
    if (self = [super initWithCoder:aDecoder]) {
        
        [self createContentView];
    }
    
    return self;
}


#pragma mark Getter Method 
-(UIView *)customShareView{
    
    if (!_customShareView) {
        
        _customShareView = [[UIView alloc] initWithFrame:CGRectMake(0, HEIGHT, WIDTH, SHARE_VIEW_HEIGHT)];
        _customShareView.backgroundColor = [UIColor whiteColor];
        _customShareView.userInteractionEnabled =YES;
        [self createButtons];
        
    }
    
    return _customShareView;
}


#pragma mark Privite Method
-(void)createContentView
{
    self.frame = CGRectMake(0, 0, WIDTH, HEIGHT);
    self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.2];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hiden)];
    [self addGestureRecognizer:tap];
    
    [self addSubview:self.customShareView];
    
}


#pragma mark 创建分享按钮
-(void)createButtons
{
    for (int i =0;i <self.imageArray.count;i++) {
        
        double width = WIDTH/3;
        double x=(i%3)*width;
        double height = (SHARE_VIEW_HEIGHT - 60)/2;
        double y=(i/3)*height;
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(x, y, width, height);
        btn.tag  = SHAREBUTTON_TAG+i;
        [btn setImage:[UIImage imageNamed:self.imageArray[i]] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(shareEvent:) forControlEvents:UIControlEventTouchUpInside];
        [self.customShareView addSubview:btn];
        
        UILabel *titleLable = [[UILabel alloc] init];
        titleLable.text = self.titleArray[i];
        titleLable.textColor = [UIColor colorWithRed:80.0/255.0 green:80.0/255.0 blue:80.0/255.0 alpha:1];
        titleLable.textAlignment = NSTextAlignmentCenter;
        titleLable.font = [UIFont systemFontOfSize:14];
        [btn addSubview:titleLable];
        titleLable.frame = CGRectMake(0, btn.frame.size.height-20,btn.frame.size.width, 20);
        
    }
    
    UIView *lineView= [[UIView alloc] initWithFrame:CGRectMake(0, SHARE_VIEW_HEIGHT-50,WIDTH, 0.5)];
    lineView.backgroundColor = [UIColor colorWithRed:201.0/255 green:201.0/255 blue:201.0/255 alpha:1];
    [self.customShareView addSubview:lineView];
    

    //取消按钮
    UIButton  *cancleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    cancleBtn.frame = CGRectMake(0, SHARE_VIEW_HEIGHT-49,WIDTH, 49);
    [cancleBtn setBackgroundImage:[self imageWithColor:[UIColor whiteColor]] forState:UIControlStateNormal];
    [cancleBtn setBackgroundImage:[self imageWithColor:[UIColor colorWithRed:201.0/255 green:201.0/255 blue:201.0/255 alpha:1]] forState:UIControlStateHighlighted];
    [cancleBtn setTitle:@"取消" forState:UIControlStateNormal];
    cancleBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [cancleBtn setTitleColor:[UIColor colorWithRed:128.0/255 green:128.0/255 blue:128.0/255 alpha:1] forState:UIControlStateNormal];
    [cancleBtn addTarget:self action:@selector(hiden) forControlEvents:UIControlEventTouchUpInside];
    [self.customShareView addSubview:cancleBtn];
    
}


#pragma mark 根据UIColor生成UIImage
- (UIImage *)imageWithColor:(UIColor *)color
{
    return [self imageWithColor:color size:CGSizeMake(1.f, 1.f)];
}


#pragma mark 根据UIColor生成指定大小的UIImage
- (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size
{
    CGRect rect = CGRectMake(0.0f, 0.0f, size.width, size.height);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    
    UIImage * theImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return theImage;
}


#pragma mark - Public Method
#pragma mark 点击分享
-(void)shareEvent:(UIButton*)btn
{
    
    self.clickblocks(btn.tag - SHAREBUTTON_TAG);
    [self hiden];
    
}


#pragma mark 展示分享视图
-(void)show
{
      UIWindow *windowView = [[[UIApplication sharedApplication] delegate] window];
      [windowView addSubview:self];
    
      [UIView animateWithDuration:showInterVal animations:^{
          
          self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.4];
          self.customShareView.frame = CGRectMake(0, HEIGHT - SHARE_VIEW_HEIGHT, WIDTH,SHARE_VIEW_HEIGHT);
          
      } completion:^(BOOL finished) {
          
      }];
}


#pragma mark 隐藏分享视图
-(void)hiden
{
    [UIView animateWithDuration:showInterVal animations:^{
        
        self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0];
        self.customShareView.frame = CGRectMake(0, HEIGHT, WIDTH,SHARE_VIEW_HEIGHT);
        
    } completion:^(BOOL finished) {
        
        [self removeFromSuperview];
        
    }];

}



@end
